<?php

/**
 * 
 * @version $Id: Resource.php 5257 2012-12-26 03:42:39Z nhudt $
 * @author dangnh6
 *
 */
class SGN_Resource extends Zend_Registry
{
    /**
     *
     * Configuration instances
     *
     * @var array
     */
    private static $_configs;
    /**
     * Resource cache
     * 
     * @var string
     */
    private static $_resource;
    /**
     * Get resource path with version
     * 
     * @param string $module
     * @param string $file
     * @param int $version
     * 
     * @return integer
     */
    public static function getPath($module, $file = '', $folder = "module", $version = 0)
    {
        /*
         * if path has already existed, return path
         */
        if(!empty($file) && isset(self::$_configs[$folder.$module.$file])) {
            return self::$_configs[$folder.$module.$file];
        } else if (isset(self::$_configs[$folder.$module]) && empty($file)) {
            return self::$_configs[$folder.$module];
        }
        /*
         * Resource
         */
        if (!self::$_resource) {
            self::$_resource = SGN_Application::getConfig('sgn', 'site')->resource;
        }
        /*
         * set $file and $version
         */
        if(!empty($file)) {
            self::$_configs[$folder.$module.$file] = self::$_resource . "/" . $folder ."/" . $module . $file . "?v=" . ($version ? $version : self::getStaticFileVersion());
            //Add subfix .min.js
            $url = self::$_configs[$folder.$module.$file];
            if($folder != 'third-party' && strpos($url, '.min.js') === false){
//             if($folder != 'third-party' && strpos($url, '.min.js') === false && !SGN_Application::isInternalUser()){
                self::$_configs[$folder.$module.$file] = str_replace('.js', '.min.js', self::$_configs[$folder.$module.$file]);
            }
            return self::$_configs[$folder.$module.$file];
        } else {
            self::$_configs[$folder.$module] = self::$_resource . "/" . $folder . "/" . $module;
            return self::$_configs[$folder.$module];
        }
        
    }
    /**
     * get static version
     * 
     * @return integer
     */
    public static function getStaticFileVersion()
    {
        $versionConfig = SGN_Application::getConfig('default', 'global');
        return $versionConfig->staticVersion;
    }
}